﻿using FaceWebServer.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ruanmou.Redis.Service;
using FaceWebServer.DB.Device;
using FaceWebServer.Model.People;
using FaceWebServer.DB;
using FaceWebServer.DB.TimeGroup;

namespace FaceWebServer.Service
{
    public class CacheService : BaseService, ICacheService
    {
        public CacheService(DbContext context) : base(context)
        {
        }

        public bool LoadCache()
        {
            var sKey = string.Empty;

            using (var redis = new RedisHashService())
            {
                //加载所有设备列表
                var devices = Query<FaceDeviceDetail>(x => true).ToList();
                //redis.RemoteKey("FC8280TServer:Device");

                foreach (var device in devices)
                {
                    sKey = $"FC8280TServer:Device:{device.DeviceID}";
                    redis.RemoteKey(sKey);
                    redis.StoreAsHash(sKey, device);

                    sKey = $"FC8280TServer:Access:{device.DeviceID}";
                    redis.RemoteKey(sKey);
                    redis.SetEntryInHash(sKey, "AccessTotal", "0");
                    redis.SetEntryInHash(sKey, "NewTotal", "0");
                    redis.SetEntryInHash(sKey, "DeleteTotal", "0");
                    redis.SetEntryInHash(sKey, "TaskTotal", "0");

                    sKey = $"FC8280TServer:RemoteTask:{device.DeviceID}";
                    redis.RemoteKey(sKey);
                    

                }
                devices = null;

                //加载所有的人员
                var peoples = Query<FacePeople>(x => true).ToList();
                foreach (var people in peoples)
                {
                    sKey = $"FC8280TServer:People:{people.EmployeeID}";
                    redis.RemoteKey(sKey);
                    redis.StoreAsHash(sKey, people);
                }
                peoples = null;
                //加载设备权限统计数
                var query = from device in Set<FaceDeviceDetail>()
                            join dAccess in Set<FacePeopleAccessDetail>()
                            on device.ID equals dAccess.DeviceID
                            where dAccess.UploadStatus == 0
                            group device by device.DeviceID into groupedItems
                            select new { SN = groupedItems.Key, Count = groupedItems.Count() }
                            ;
                foreach (var totalitem in query)
                {
                    sKey = $"FC8280TServer:Access:{totalitem.SN}";
                    redis.SetEntryInHash(sKey, "NewTotal", totalitem.Count.ToString());
                }
                //查询待删除
                query = from device in Set<FaceDeviceDetail>()
                        join dAccess in Set<FacePeopleAccessDetail>()
                        on device.ID equals dAccess.DeviceID
                        where dAccess.UploadStatus == 2
                        group device by device.DeviceID into groupedItems
                        select new { SN = groupedItems.Key, Count = groupedItems.Count() }
                            ;
                foreach (var totalitem in query)
                {
                    sKey = $"FC8280TServer:Access:{totalitem.SN}";
                    redis.SetEntryInHash(sKey, "DeleteTotal", totalitem.Count.ToString());
                }

                //查询所有
                query = from device in Set<FaceDeviceDetail>()
                        join dAccess in Set<FacePeopleAccessDetail>()
                        on device.ID equals dAccess.DeviceID
                        where dAccess.UploadStatus < 2
                        group device by device.DeviceID into groupedItems
                        select new { SN = groupedItems.Key, Count = groupedItems.Count() }
                            ;
                foreach (var totalitem in query)
                {
                    sKey = $"FC8280TServer:Access:{totalitem.SN}";
                    redis.SetEntryInHash(sKey, "AccessTotal", totalitem.Count.ToString());
                }
               
                //加载所有任务
                query = from device in Set<FaceDeviceDetail>()
                        join dTask in Set<RemoteTaskDetail>()
                        on device.ID equals dTask.DeviceID
                        where dTask.TaskStatus == 0
                        group device by device.DeviceID into groupedItems
                        select new { SN = groupedItems.Key, Count = groupedItems.Count() };
                foreach (var totalitem in query)
                {
                    sKey = $"FC8280TServer:Access:{totalitem.SN}";
                    redis.SetEntryInHash(sKey, "TaskTotal", totalitem.Count.ToString());
                }
                query = null;


                //加载所有开门时段
                TimeGroupService timeGroupService = new TimeGroupService(Context);

                var TimeGroupDetails= timeGroupService.GetAll();
                if(TimeGroupDetails.Count != 64)
                {
                    //初始化开门时段
                    
                    timeGroupService.IniTimeGroupDB();
                }

            }

            return true;
        }
    }
}
